import 'package:boss/color_constant.dart';
import 'package:boss/common/common.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/commodity/categories/page/searchCommodity/search_commodity_provider.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flustars/flustars.dart' as flustars;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class SearchCommodityHistory extends StatefulWidget {
  @override
  _SearchCommodityHistoryState createState() => _SearchCommodityHistoryState();
}

class _SearchCommodityHistoryState extends State<SearchCommodityHistory> {
  @override
  Widget build(BuildContext context) {
    var chips = flustars.SpUtil.getStringList(Constant.spSearchCommodity);

    return Container(
      padding: EdgeInsets.symmetric(
        horizontal: 30.w,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(
                '历史搜索',
                style: TextStyleConstant().normal_3_26,
              ),
              GestureDetector(
                child: Padding(
                  padding: EdgeInsets.all(8.w),
                  child: BaseImage(
                    'assets/image/delete_icon.png',
                    width: 34.w,
                    height: 34.w,
                  ),
                ),
                behavior: HitTestBehavior.translucent,
                onTap: () async {
                  await flustars.SpUtil.remove(Constant.spSearchCommodity);
                  setState(() {});
                },
              ),
            ],
          ),
          Wrap(
            spacing: 10.w,
            runSpacing: 10.w,
            children: chips
                .map((item) => GestureDetector(
                      onTap: () {
                        var provider = Provider.of<SearchCommodityProvider>(
                          context,
                          listen: false,
                        );
                        var commodity = provider.commoditys.firstWhere(
                          (temp) => temp.title == item,
                          orElse: () => null,
                        );
                        Navigator.pop(context, commodity);
                      },
                      child: Chip(
                        label: Text(item),
                        backgroundColor: ColorConstant.f7f7,
                        labelStyle: TextStyleConstant().normal_2_24,
                        padding: EdgeInsets.symmetric(
                          horizontal: 18.w,
                        ),
                      ),
                    ))
                .toList(),
          ),
        ],
      ),
    );
  }
}
